using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace GoogleNet
{
    /// <summary>
    /// 
    /// </summary>
    public class KmlListStyle : IKmlElement
    {
        private string tagName = "ListStyle";

        #region BgColor
        private Color bgColor;

        /// <summary>
        /// <para>
        /// Background color for the Snippet (optional). Color and opacity (alpha) values are expressed 
        /// in hexadecimal notation. The range of values for any one color is 0 to 255 (00 to ff). 
        /// The order of expression is aabbggrr, where aa=alpha (00 to ff); bb=blue (00 to ff); 
        /// gg=green (00 to ff); rr=red (00 to ff). For alpha, 00 is fully transparent and ff is fully opaque. 
        /// </para>
        /// <para>
        /// For example, if you want to apply a blue color with 50 percent opacity to an overlay, 
        /// you would specify the following: &lt;bgColor&gt;7fff0000&lt;/bgColor&gt;, where alpha=0x7f, blue=0xff, green=0x00, and red=0x00. 
        /// </para> 
        /// </summary>
        /// <value>The color.</value>
        public Color BgColor
        {
            get { return bgColor; }
            set { bgColor = value; }
        }
        #endregion

        #region ListItemType
        ListItemType listItemType = ListItemType.check;

        /// <summary>
        /// Specifies how a Folder is displayed in the list view.
        /// </summary>
        /// <value>The type of the list item.</value>
        public ListItemType ListItemType
        {
            get { return listItemType; }
            set { listItemType = value; }
        } 
        #endregion

        #region ItemIcon
        private KmlItemIcon itemIcon;

        /// <summary>
        /// Icon used in the List view that reflects the state of a Folder or Link fetch. 
        /// Icons associated with the open and closed modes are used for Folders. 
        /// Icons associated with the error and fetching0, fetching1, and fetching2 modes are used for Network Links. 
        /// </summary>
        /// <value>The item icon.</value>
        public KmlItemIcon ItemIcon
        {
            get { return itemIcon; }
            set { itemIcon = value; }
        } 
        #endregion


        #region IKmlElement Members

        /// <summary>
        /// Name of the KML tag for this specific element.
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        public string TagName
        {
            get { return tagName; }
        }

        #endregion

        #region IKmlGenerator Members

        /// <summary>
        /// Generates the KML for this element including its child elements.
        /// </summary>
        /// <param name="writer">The XML writer that will be used to generate the KML.</param>
        public void GenerateKml(System.Xml.XmlTextWriter writer)
        {
            writer.WriteStartElement(tagName);

            if (bgColor != null)
                writer.WriteElementString("bgColor", convertColorToKmlColor(bgColor));
            
            writer.WriteElementString("listItemType", listItemType.ToString());

            if (itemIcon != null)
                itemIcon.GenerateKml(writer);

            writer.WriteFullEndElement();
        }

        #endregion

        /// <summary>
        /// Converts the .NET color to its KML's hexadecimal representation.
        /// </summary>
        /// <param name="c">The color to convert</param>
        /// <returns></returns>
        private string convertColorToKmlColor(Color c)
        {
            string hexColor = string.Format("{0:x2}", c.A) +
                                string.Format("{0:x2}", c.B) +
                                string.Format("{0:x2}", c.G) +
                                string.Format("{0:x2}", c.R);

            return hexColor;
        }
    }
}
